package com.zcx.mall.oauth.service.impl;

import com.zcx.mall.common.web.exception.BizExcepton;
import com.zcx.mall.oauth.config.RedisService;
import com.zcx.mall.oauth.constant.OauthBizConstant;
import com.zcx.mall.oauth.constant.RedisConstant;
import com.zcx.mall.oauth.entity.SmsEntity;
import com.zcx.mall.oauth.service.SmsService;
import com.zcx.mall.oauth.vo.SmsResult;
import com.zcx.mall.oauth.vo.ValidateSmsRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class SmsServiceImpl implements SmsService {


    @Autowired
    RedisService redisService;

    @Override
    public SmsResult sendSms(String mobile, OauthBizConstant.SmsType type) {
        String key =  RedisConstant.SMS_PREFIX + mobile + "_" + type.getValue();
        SmsEntity smsEntity = (SmsEntity) redisService.get(key);
        if (smsEntity != null) {
            if (smsEntity.getTryDate().getTime() > new Date().getTime()) {
                throw new BizExcepton("60秒后才能再次获取短信");
            }
        }
        String smsNo = UUID.randomUUID().toString();
        smsEntity = new SmsEntity();
        smsEntity.setSmsNo(smsNo);
        smsEntity.setMobile(mobile);
        smsEntity.setTryDate(new Date(new Date().getTime() + 60000));
        smsEntity.setValidateDate(new Date(new Date().getTime() + 360000));
        smsEntity.setCreateDate(new Date());
        //未验证
        smsEntity.setValidateState(OauthBizConstant.Whether.WAIT.getValue());
        //有
        redisService.set(key, smsEntity, 3600);
        SmsResult smsResult = new SmsResult();
        BeanUtils.copyProperties(smsEntity, smsResult);
        return smsResult;
    }

    @Override
    public void validateSms(ValidateSmsRequest validateSmsRequest) {
        String key =  RedisConstant.SMS_PREFIX + validateSmsRequest.getMobile() + "_" + validateSmsRequest.getType();
        SmsEntity smsEntity = (SmsEntity) redisService.get(key);
        if (smsEntity == null) {
            throw new BizExcepton("验证码已失效");
        }
        boolean validate = true;
        //判断验证码
        try {
            if (!OauthBizConstant.Whether.WAIT.getValue().equals(smsEntity.getValidateState())) {
                throw new BizExcepton("验证码已失效");
            }
            if (smsEntity.getValidateDate().getTime() < new Date().getTime()) {
                throw new BizExcepton("验证码已失效");
            }
            if(!validateSmsRequest.getSmsNo().equals(smsEntity.getSmsNo())) {
                throw new BizExcepton("验证码已失效");
            }
            if(!validateSmsRequest.getMobile().equals(smsEntity.getMobile())) {
                throw new BizExcepton("验证码已失效");
            }
            if(!validateSmsRequest.getSmsCode().equals(smsEntity.getSmsCode())) {
                throw new BizExcepton("验证码不正确");
            }
        } catch (BizExcepton e) {
            validate = false;
            throw  e;
        } finally {
            if (validate) {
                smsEntity.setValidateState(OauthBizConstant.Whether.SUC.getValue());
            } else {
                smsEntity.setValidateState(OauthBizConstant.Whether.FAIL.getValue());

            }
            redisService.set(key, smsEntity, 3600);
        }

    }

    @Override
    public void confirmSms(String smsNo, String mobile, OauthBizConstant.SmsType type) {
        String key =  RedisConstant.SMS_PREFIX + mobile + "_" + type.getValue();
        SmsEntity smsEntity = (SmsEntity) redisService.get(key);
        if (smsEntity == null) {
            throw new BizExcepton("验证码已失效");
        }
        if(!smsNo.equals(smsEntity.getSmsNo())) {
            throw new BizExcepton("验证码已失效");
        }
        if(!mobile.equals(smsEntity.getMobile())) {
            throw new BizExcepton("验证码已失效");
        }
        if (!OauthBizConstant.Whether.SUC.getValue().equals(smsEntity.getValidateState())) {
            throw new BizExcepton("短信验证码未验证");
        }
    }
}
